package com.can.pool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

//Executors 工具类、3大方法
//下面这些方法不安全,要自定义线程池new ThreadPoolExecutor
public class Executors01 {
    public static void main(String[] args) {
        //单个线程
        ExecutorService threadPool = Executors.newSingleThreadExecutor();
        //创建一个固定的线程池的大小
        ExecutorService threadPool2 = Executors.newFixedThreadPool(5);
        //可伸缩的，遇强则强，遇弱则弱
        ExecutorService threadPool3 = Executors.newCachedThreadPool();

        try {
            for (int i = 0; i < 100; i++) {
                //使用了线程池之后，要使用线程池来创建线程
                threadPool.execute(()->{
                    System.out.println(Thread.currentThread().getName()+" OK");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //线程池用完，程序结束，关闭线程池
            threadPool.shutdown();
        }
    }
}
